<?php

/**
 * HTTP响应类
 *
 * @category   ORG
 * @package    ORG
 * @subpackage RestClient
 * @author     海啸 <459103503@qq.com>
 */
class JsonClient extends RestClient
{
    /**
     * POST请求
     *
     * @param string $url     资源URL
     * @param mixed  $data    Post data
     * @param array  $headers Headers
     *
     * @return string
     */
    public function post($url, $data, $headers=array())
    {
        return parent::post($url, json_encode($data), $headers);
    }

    /**
     * PUT请求
     *
     * @param string $url     资源URL
     * @param mixed  $data    Put data
     * @param array  $headers Header
     *
     * @return string
     */
    public function put($url, $data, $headers=array())
    {
        return parent::put($url, json_encode($data), $headers);
    }

    /**
     * 获取预提交数据
     *
     * @param array  $opts Options
     * @param string $url  Resource URL
     *
     * @return object
     */
    protected function prepRequest($opts, $url)
    {
        $opts[CURLOPT_HTTPHEADER][] = 'Accept: application/json';
        $opts[CURLOPT_HTTPHEADER][] = 'Content-Type: application/json';
        return parent::prepRequest($opts, $url);
    }

    /**
     * 获取body信息
     *
     * @param string $body Response body
     *
     * @return string
     */
    public function processBody($body)
    {
        return json_decode($body, true);
    }
}